﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication2
{
    public partial class bloqalum : Form
    {
        public bloqalum()
        {
            InitializeComponent();
        }
        Conexion conn = new Conexion();
        GestionFgut g = new GestionFgut();
        private void inicializarCheckbox()
        {
            for (int i = 0; i < grilla.RowCount; i++)
            {
                grilla[0, i].Value = false;
            }
        }
        public DataSet getFiltro(string rut)
        {
            conn.conectar();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("SELECT rut,nombre,fecha_nac,sexo,celular,email,telefono,estado FROM alumno WHERE RUT='" + rut + "'", conn.conector);
            da.Fill(ds, "Filtro");
            conn.desconectar();
            return ds;
        }
        public DataSet getFiltro2()
        {
            conn.conectar();
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter("SELECT rut,nombre,fecha_nac,sexo,celular,email,telefono,estado FROM alumno", conn.conector);
            da.Fill(ds, "Filtro2");
            conn.desconectar();
            return ds;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Visible = false;
            estudiante es = new estudiante();
            es.ShowDialog();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Visible = false;
            mostraralum ma = new mostraralum();
            ma.ShowDialog();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Visible = false;
            modificaralum moda = new modificaralum();
            moda.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string rutcompleto = txtrut.Text + txtdigito.Text;
            if (string.IsNullOrWhiteSpace(txtrut.Text))
            {
                MessageBox.Show("Debe Ingresar un RUT !", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                grilla.DataSource = getFiltro(rutcompleto);
                grilla.DataMember = "Filtro";
                inicializarCheckbox();
            }
        }

        private void bloqalum_Load(object sender, EventArgs e)
        {
            grilla.DataSource = getFiltro2();
            grilla.DataMember = "Filtro2";
            inicializarCheckbox();
        }

        private void txtrut_TextChanged(object sender, EventArgs e)
        {
            if (txtrut.Text == "")
            {
                txtdigito.Text = "";
            }
            if (txtrut.TextLength == 7 || txtrut.TextLength == 8)
            {
                int rut = Convert.ToInt32(txtrut.Text);
                string digito = g.digitoVerificador(rut);
                txtdigito.Text = digito;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string rutcompleto = txtrut.Text + txtdigito.Text;
            for (int i = 0; i < grilla.RowCount; i++)
            {

                if (grilla[0, i].Value.ToString() == "true")
                {
                    int codigo;
                    int.TryParse(grilla[1, i].Value.ToString(), out codigo);
                    g.eliminar(rutcompleto);
                }
            }
            grilla.DataSource = getFiltro(rutcompleto);
            grilla.DataMember = "Filtro";
            inicializarCheckbox();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string rutcompleto = txtrut.Text + txtdigito.Text;
            if (grilla[8, 0].Value.ToString().Equals("PAGADO"))
            {
                for (int i = 0; i < grilla.RowCount; i++)
                {

                    if (grilla[0, i].Value.ToString() == "true")
                    {
                        //MessageBox.Show(grilla[8, 0].Value.ToString());
                        int codigo;
                        int.TryParse(grilla[1, i].Value.ToString(), out codigo);
                        // 
                        // g.Bloquear(rutcompleto);
                        //if (grilla[8, 0].Value.ToString().Equals("NO Pagado"))
                        g.Bloquear(rutcompleto);

                    }
                }
            }
            else
            {
                for (int i = 0; i < grilla.RowCount; i++)
                {

                    if (grilla[0, i].Value.ToString() == "true")
                    {
                        //MessageBox.Show(grilla[8, 0].Value.ToString());
                        int codigo;
                        int.TryParse(grilla[1, i].Value.ToString(), out codigo);
                        // 
                        // g.Bloquear(rutcompleto);
                        //if (grilla[8, 0].Value.ToString().Equals("NO Pagado"))
                        g.desBloquear(rutcompleto);

                    }
                }
            }
            grilla.DataSource = getFiltro(rutcompleto);
            grilla.DataMember = "Filtro";
            inicializarCheckbox();
        }

        private void txtrut_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}
